// pages/login/login.js
var app = getApp();
var util = require('../../utils/util');
function checkTarbar() {
  var tarbar = [
    'pages/index/index',
    'pages/service/service',
    'pages/personal/personal',
  ];
  for (var i in tarbar) {
    if (tarbar[i] == backRoute) {
      return true;
    } else {
      return false;
    }
  }
}

var backRoute = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ispwd:false,
    username:"",
    password:""
  },
  getBack: function () {
    var that = this;
    console.log(checkTarbar())
    if (checkTarbar()) {
      console.log(backRoute)
      wx.switchTab({
        url: '/' + backRoute,
      });
    } else {
      wx.reLaunch({
        url: '/' + backRoute,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    backRoute = options.back_url != undefined ? options.back_url : 'pages/index/index';
    if (wx.getStorageSync('dis_id').length>1) {
      that.getBack();
    }
  },
 //是否记住密码  使用
  onUserName:function(e){
    this.setData({
      username: e.detail.value
    })
  },
  //是否记住密码  使用
  onPassWord: function (e) {
    this.setData({
      password: e.detail.value
    })
  },
  //是否记住密码
  pwdChecked:function(e){
    this.data.ispwd = !this.data.ispwd;
    if (this.data.ispwd){
      wx.setStorageSync("pwdInfo",{
        username: this.data.username,
        password: this.data.password
      })
    }else{
      wx.removeStorage({
        key: 'pwdInfo',
        success(res) {
          console.log(res)
        }
      })
    }
    this.setData({
      ispwd: this.data.ispwd
    })
 //   console.log(this.data.username, password);
  },
  formSubmit: function (e) {
    var that = this;
    var username = e.detail.value.username;
    if (username.length < 1) {
      util.showSuccess('请输入账号', 'none', false);
      return false;
    }
    var password = e.detail.value.password;
    if (password.length < 1) {
      util.showSuccess('请输入密码', 'none', false);
      return false;
    }
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res);
        util.showLoading();
        var apiUrl = '/api/dis/login/index_wx';
        var apiData = {
          js_code: res.code,
          username: username,
          password: password
        }
        util.fetchPost(apiUrl, apiData, function (res) {
          util.hideLoading();
          if (res.state == 1) {
            wx.setStorageSync('dis_id',res.datas.id)
            util.showSuccess('登录成功');
            setTimeout(function () {
              that.getBack();
            }, 2000);
          } else if (res.state == -3) {
            // util.confirm('骑手账号不存在');
			  util.confirm('骑手账号未注册，是否注册', function () {
				  that.onRegisterTap();
			  });
          } else if (res.state == -4){
            util.confirm('密码错误');
          }else {
            util.message(res.datas);
          }
        });
      }
    })
  },
	//跳转注册页面
	onRegisterTap: function () {
		wx.navigateTo({
			url: '../registerpage/registerpage',
		})
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})